#ifndef PARSENIT_H
#define PARSENIT_H
#include <string.h>
#include <stdio.h>

#define NIT_DESCRIPTOR_MAX 1024
#define NIT_TRANSPORT_MAX 128

typedef struct TS_NIT_TRANSPORT
{
    unsigned int transport_stream_id :16;
    unsigned int original_network_id :16;
    unsigned int reserved_future_use :4;
    unsigned int transport_descriptors_length :12;
    unsigned char descriptor[NIT_DESCRIPTOR_MAX];

} TS_NIT_TRANSPORT;

typedef struct TS_NIT
{
    unsigned int table_id :8;
    unsigned int section_syntax_indicator :1;
    unsigned int reserved_future_use_1 :1;
    unsigned int reserved_1 :2;
    unsigned int section_length :12;
    unsigned int network_id :16;
    unsigned int reserved_2 :2;
    unsigned int version_number :5;
    unsigned int current_next_indicator :1;
    unsigned int section_number :8;
    unsigned int last_section_number :8;
    unsigned int reserved_future_use_2 :4;
    unsigned int network_descriptors_length :12;
    unsigned char network_descriptor[NIT_DESCRIPTOR_MAX];
    unsigned int reserved_future_use_3 :4;
    unsigned int transport_stream_loop_length :12;

    TS_NIT_TRANSPORT astNIT_Transport[NIT_TRANSPORT_MAX];

    unsigned int CRC_32 :32;
} TS_NIT;

class ParseNIT
{
public:
    ~ParseNIT();
    ParseNIT(const ParseNIT&)=delete;
    ParseNIT& operator=(const ParseNIT&)=delete;
    static ParseNIT& get_instance();
    /*********************************************************************************************************************************
     * Function Name : ParseNIT_Table
     * Description   :  parse the PAT table and get the information
     * Parameters    :
     * 				pfTsFile -- the TS file descriptor
     * 				iTsPosition -- the TS package begin position
     *				iTsLength -- the TS package length
     * Returns       : -1 failure 0 -- no the section in the transport stream 1 - all section over
     *********************************************************************************************************************************/
    int ParseNIT_Table(FILE *pfTsFile, int iTsPosition, int iTsLength);
private:
    /*********************************************************************************************************************************
     * Function Name : ParseSDT_SectionHead
     * Description   :  parse the SDT section head
     * Parameters    :
     * 				pstTS_SDT -- the TS_SDT
     *				pucSectionBuffer -- the SDT section buffer
     * Returns       : void
     *********************************************************************************************************************************/
    void ParseNIT_SectionHead(TS_NIT *pstTS_NIT, unsigned char *pucSectionBuffer);
    /*********************************************************************************************************************************
     * Function Name : ParseNIT_Section
     * Description   : parse the NIT table
     * Parameters    :
     *				pTS_NIT -- the TS_NIT
     *				pucSectionBuffer -- the buffer of NIT section
     * Returns       : the count of TS_NIT_TRANSPORT
     *********************************************************************************************************************************/
    int ParseNIT_Section(TS_NIT *pstTS_NIT, unsigned char *pucSectionBuffer);
    /*********************************************************************************************************************************
     * Function Name : DeleteHex0x
     * Description   : delete the hex 0x for example(0x47(hex) -> 47(decimal))
     * Parameters    :
     *				ucSource -- the hex value
     * Returns       : the decimal value delete the 0x
     *********************************************************************************************************************************/
    int DeleteHex0x(unsigned char ucSource);
    /*********************************************************************************************************************************
     * Function Name : PrintSatelliteInfo
     * Description   : print the satellite information
     * Parameters    :
     *				ucDescriptor -- the descriptor buffer
     *				iDescriprotLength -- the descriptor length
     * Returns       : void
     *********************************************************************************************************************************/
    void PrintSatelliteInfo(unsigned char *pucDescriptor, unsigned int uiDescriprotLength);

    /*********************************************************************************************************************************
     * Function Name : PrintServiceListInfo
     * Description   : print the information in the service_list_descriptor
     * Parameters    :
     *				ucDescriptor -- the descriptor buffer
     *				iDescriprotLength -- the descriptor length
     * Returns       : void
     *********************************************************************************************************************************/
    void PrintServiceListInfo(unsigned char *pucDescriptor, unsigned int uiDescriprotLength);
    /*********************************************************************************************************************************
     * Function Name : PrintNetworkName
     * Description   : print the network name information
     * Parameters    :
     *				pucDescriptor -- the descriptor buffer
     *				uiDescriprotLength -- the descriptor length
     * Returns       : void
     *********************************************************************************************************************************/
    void PrintNetworkName(unsigned char *pucDescriptor, unsigned int uiDescriprotLength);
    /*********************************************************************************************************************************
     * Function Name : PrintNIT_Info
     * Description   : print the NIT information
     * Parameters    :
     *				pstTS_NIT -- the TS_NIT
     *				iTranportCount -- the count of the transport
     * Returns       : void
     *********************************************************************************************************************************/
    void PrintNIT_Info(TS_NIT *pstTS_NIT, int iTranportCount);
private:
    ParseNIT();
};

#endif // PARSENIT_H
